$(function () {
    /*加载有机商品的异步请求*/
    $.ajax({
        url: "queryCommend.do",
        type: "GET",
        dataType: "JSON",
        success: function (resp) {
            $.each(resp, function (i, n) {
                $("#row").append("                <div class=\"col-xl-3 col-lg-4 col-sm-6\">\n" +
                    "                    <div class=\"product-item\">\n" +
                    "                        <div class=\"product-img img-zoom-effect\">\n" +
                    "                            <a href=\"goSingle.do?id=" + n.id + "\">\n" +
                    "                                <img class=\"img-full\" src=\"" + "upload/" + n.filename + "\"\n" +
                    "                                     alt=\"Product Images\">\n" +
                    "                            </a>\n" +
                    "                            <div class=\"product-add-action\">\n" +
                    "                                <ul>\n" +
                    "                                    <li>\n" +
                    "                                        <a onclick='sengAsyn(" + n.id + ")' href=\"javascript:void(0);\">\n" +
                    "                                            <i class=\"pe-7s-cart\"></i>\n" +
                    "                                        </a>\n" +
                    "                                    </li>\n" +
                    "                                    <li>\n" +
                    "                                        <a href=\"order.do?goodsId=" + n.id + "\">\n" +
                    "                                            <i class=\"pe-7s-shuffle\"></i>\n" +
                    "                                        </a>\n" +
                    "                                    </li>\n" +
                    "                                    <li>\n" +
                    "                                        <a href=\"compare.do?id=" + n.id + "\">\n" +
                    "                                            <i class=\"pe-7s-like\"></i>\n" +
                    "                                        </a>\n" +
                    "                                    </li>\n" +
                    "                                </ul>\n" +
                    "                            </div>\n" +
                    "                        </div>\n" +
                    "                        <div class=\"product-content\">\n" +
                    "                            <a class=\"product-name\" href=\"single-product.html?id=" + n.id + "\">" + n.goodsName + "</a>\n" +
                    "                            <div class=\"price-box pb-1\">\n" +
                    "                                <span class=\"new-price\">" + "￥" + n.price + "</span>\n" +
                    "                            </div>\n" +
                    "                            <div class=\"rating-box\">\n" +
                    "                                <ul>\n" +
                    "                                    <li><i class=\"pe-7s-star\"></i></li>\n" +
                    "                                    <li><i class=\"pe-7s-star\"></i></li>\n" +
                    "                                    <li><i class=\"pe-7s-star\"></i></li>\n" +
                    "                                    <li><i class=\"pe-7s-star\"></i></li>\n" +
                    "                                    <li><i class=\"pe-7s-star\"></i></li>\n" +
                    "                                </ul>\n" +
                    "                            </div>\n" +
                    "                        </div>\n" +
                    "                    </div>\n" +
                    "                </div>\n")
            })
        }
    })
    /**
     * 查询最新发布的商品
     */
    $.ajax({
        url: "queryNewGoods.do",
        type: "GET",
        dataType: "JSON",
        success: function (resp) {
            $.each(resp, function (i, n) {
                $("#goodsItem").append("                    <div class=\"col-lg-4 col-md-6\">\n" +
                    "                        <div class=\"banner-item\">\n" +
                    "                            <div class=\"banner-img img-zoom-effect\">\n" +
                    "                                <img style='width: 320px;height: 265px;' src=\"upload/"+ n.filename +"\" alt=\"Banner Image\">\n" +
                    "                                <div class=\"inner-content\">\n" +
                    "                                    <h5 class=\"offer\">-10% 折扣</h5>\n" +
                    "                                    <h5 class=\"title mb-5\">"+ n.goodsName +"<br>"+ n.price +"</h5>\n" +
                    "                                    <div class=\"button-wrap\"> <br> <br> <br> <br>\n" +
                    "                                        <a class=\"btn btn-primary btn-white-hover rounded-0\" href=\"order.do?goodsId="+ n.id +"\">立即购买</a>\n" +
                    "                                    </div>\n" +
                    "                                </div>\n" +
                    "                            </div>\n" +
                    "                        </div>\n" +
                    "                    </div>\n")
            })
        }
    })
    /*计算总价*/
    $.ajax({
        url:"findTotalPrice.do",
        dataType:"JSON",
        type:"POST",
        success:function (resp) {
            $("#totalPri").text(resp)
        }
    })
})

/*点击超链接*/
function sengAsyn(id){
    $.ajax({
        url:"addCart.do",
        data:{
            id:id,
            num:1
        },
        dataType:"JSON",
        type:"POST",
        success:function (response) {
            window.alert(response.info);
        }
    })
}
